# Screen - This screen contains a list of entries in one feed.
# Copyright (C) 2007 Lauri Taimila
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2007, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

import os
import gtk
import pango
import clutter

# Widgets
from frontend.gui.widgets.text_menu import TextMenu
from frontend.gui.widgets.text_menu_item import TextMenuItem
from frontend.gui.widgets.list_indicator import ListIndicator

from utils.feed_utils import FeedEntryParser

from frontend.user_event import UserEvent
from frontend.medialibrary.feeds import Feed
from frontend.gui.screens.screen import Screen
from frontend.gui.transitions.transition import Transition

class FeedScreen(Screen):
    """
    Screen
    
    This screen is displayed when frontend is opened. Screen includes menu
    that allows user to navigate media center. This is main screen of the
    frontend.
    """
    
    def __init__(self, ui, frontend, feed):
        """
        Initialize screen
        
        @param frontend: FrontendClient object
        @param theme: Theme object
        @param feed: Feed object
        """
        self.group = clutter.Group()
        Screen.__init__(self, ui.STAGE_WIDTH, ui.STAGE_HEIGHT)
        
        self.ui = ui
        self.frontend = frontend
        self.theme = frontend.getTheme()
        self.feed = feed
        
        self._create_entry_menu()
        
        # Screen Title (Displayed at the bottom left corner)
        screen_title = clutter.Label()
        screen_title.set_font_name(self.theme.getFont() + " " + str(self.ui.y(0.13)) + "px")
        screen_title.set_color(self.theme.getColor("screentitle"))
        screen_title.set_text("Feed")
        screen_title.set_name("screen_title")
        screen_title.set_position(self.ui.x(0), self.ui.y(0.87))
        screen_title.show()
        self.group.add(screen_title)
        
        # RSS Icon
        rss = gtk.gdk.pixbuf_new_from_file(self.theme.getImage("rss_icon"))
        icon = clutter.Texture()
        icon.set_pixbuf(rss)
        icon.set_position(self.ui.x(0.05), self.ui.y(0.07))
        icon.show()
        self.group.add(icon)
        
        # RSS Feed Title
        title = clutter.Label()
        title.set_font_name(self.theme.getFont() + " " + str(self.ui.y(0.0625)) + "px")
        title.set_font_name(self.theme.getFont() + ' 48px')
        title.set_use_markup(True)
        title.set_color(self.theme.getColor("title"))
        title.set_text(self.feed.get_title())
        title.set_width(self.ui.x(0.5124))
        title.set_text(FeedEntryParser().convert(self.feed.get_title()))
        title.set_width(700)
        title.set_line_wrap(False)
        title.set_ellipsize(pango.ELLIPSIZE_END)
        title.set_position(self.ui.x(0.13), self.ui.y(0.06))
        title.show()
        self.group.add(title)
        
        # RSS Feed descrption
        desc = clutter.Label()
        desc.set_font_name(self.theme.getFont() + " " + str(self.ui.y(0.03646)) + "px")
        desc.set_color(self.theme.getColor("subtitle"))
        desc.set_text(self.feed.get_subtitle())
        desc.set_position(self.ui.x(0.13), self.ui.y(0.14))
        desc.set_use_markup(True)
        desc.set_text(FeedEntryParser().convert(self.feed.get_subtitle()))
        desc.set_position(self.frontend.getUserInterface().get_abs_x_pos(0.13), 
                           self.frontend.getUserInterface().get_abs_y_pos(0.14))
        desc.show()
        self.group.add(desc)
        
        # Last update
        date_text = self.feed.get_time()[:5]  + " - " +  self.feed.get_date()
        date = clutter.Label()
        date.set_font_name(self.theme.getFont() + " " + str(self.ui.y(0.0313)) + "px")
        date.set_color(self.theme.getColor("subtitle"))
        date.set_text(date_text)
        date.set_position(self.ui.x(0.78), self.ui.y(0.14))
        date.show()
        self.group.add(date)
        
        # Last update
        update = clutter.Label()
        update.set_font_name(self.theme.getFont() + " " + str(self.ui.y(0.0313)) + "px")
        update.set_color(self.theme.getColor("title"))
        update.set_text("Updated")
        update.set_position(self.ui.x(0.78), self.ui.y(0.1))
        update.show()
        self.group.add(update)
        
        #List indicator
        self.li = ListIndicator(self.ui.x(0.1464), self.ui.y(0.04427), self.theme, ListIndicator.VERTICAL)
        self.li.set_position(self.ui.x(0.7562) - self.li.get_width(),
                             self.ui.STAGE_HEIGHT - self.li.get_height() - self.ui.y(0.03906))
        self.li.set_delimiter(" | ")
        self.li.show()
        self.li.set_maximum(self.menu.get_number_of_items())
        self.li.set_current(1)
        self.group.add(self.li)
        
        self.group.show()
        self.frontend.getUserInterface().getStage().add(self.group)
    
    def _create_entry_menu(self):
        """
        Create Entries-menu. This menu contains list of entries.
        """
        self.menu = TextMenu(self.theme, 
                               self.frontend.get_configuration().show_effects())
        self.menu.set_row_count(1)
        self.menu.set_item_size(self.ui.x(0.5124), self.ui.y(0.0781))
        self.menu.set_position(self.ui.x(0.2438), self.ui.y(0.2604))
        
        # Create menu items based on FeedLibrary
        entries = self.feed.get_entries()
        for entry in entries:        
            item = TextMenuItem(self.ui.x(0.5124), self.ui.y(0.0781), self.theme, entry.get_title())
            item = TextMenuItem(700, 60, self.theme, FeedEntryParser().strip_tags(entry.get_title()))
            item.set_userdata(entry)
            self.menu.add_actor(item)
                
        self.menu.show()
        self.menu.set_active(True)
        self.group.add(self.menu)
    
    def get_group(self):
        """
        Get all actors of the screen.
        @return clutter.Group 
        """
        return self.group

    def get_type(self):
        """Return screen type."""
        return Screen.NORMAL
    
    def get_name(self):
        """Return screen name (human readble)"""
        return "FeedScreen"
    
    def handle_user_event(self, event):   
        """ 
        Handle screen specific user events
        @param event: UserEvent object
        """
        type = event.get_type()
        
        if type == UserEvent.NAVIGATE_UP:
            self.menu.move(TextMenu.UP)
            self.li.set_current(self.menu.get_current_postition() + 1)
            
        elif type == UserEvent.NAVIGATE_DOWN:
            self.menu.move(TextMenu.DOWN)
            self.li.set_current(self.menu.get_current_postition() + 1)
            
        elif type == UserEvent.NAVIGATE_SELECT:
            entry = self.menu.get_current_menuitem().get_userdata()
            self.ui.changeScreen(self.ui.createScreen("entry", [self.feed, entry]), 
                                 Transition.FORWARD)
